var c = require('./config.js');
var s = require('./sign.js');
var app = getApp();

const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

function request(data = {}, method = "POST", url = '') {
    wx.showLoading({
        title: '加载中',
    })
    var url = c.api_url;
    data['partner'] = '5500';
    data['sign'] = s.sign(data);
    return new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            data: data,
            method: method,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            success: function (res) {
                wx.hideLoading();
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    reject(res.errMsg);
                }
            },
            fail: function (err) {
                wx.hideLoading();
                reject(err)
            }
        })
    });
}
function requests(data = {}, method = "POST", url = '') {
    var url = c.api_url;
    data['partner'] = '5500';
    data['sign'] = s.sign(data);
    return new Promise(function (resolve, reject) {
        wx.request({
            url: url,
            data: data,
            method: method,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            success: function (res) {
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    reject(res.errMsg);
                }
            },
            fail: function (err) {
                reject(err)
            }
        })
    });
}
function checkLogin() {
    var userinfo = wx.getStorageSync("userinfo");
    if (userinfo.length == 0) {
        wx.switchTab({
            url: '/pages/my/usercenter/index'

        })
        return false;
    }
    return true;
}

function r(url) {
    wx.navigateTo({
        url: url,
    })
}


//同步openid数据
function userthree(openId) {
    var s_openid = wx.getStorageSync("s_openid");
    if (!s_openid) {
        s_openid = "";
        return false;
    }
    var data = {
        url: "/v1/userthree/edit",
        openid: openId,
        type: 0,
        s_openid: s_openid,
        s_user_id: ""
    };
    console.log(data);
    
    request(data).then(function (res) {
        if (res.code == 0) {
            var userinfo = res.data;
            userinfo = userinfo ? userinfo : [];
            wx.setStorageSync("userinfo", userinfo);
        } else {
        }
    });
}

//更新本地存储
function updateUserinfo() {
    var openId = wx.getStorageSync("openId");
    var data = {
        url: "/v1/user/gettoken",
        openid: openId,
        type: 0,
    };
    requests(data).then(function (res) {
        if (res.code == 0) {
            var userinfo = res.data;
            userinfo = userinfo ? userinfo : [];
            wx.setStorageSync("userinfo", userinfo);
        } else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1500
            })
        }
    });
}
function errMsg(msg) {
    wx.showToast({
        title: msg,
        icon: "none"
    })
}
function sucMsg(msg) {
    wx.showToast({
        title: msg,
        icon: "success"
    })
}
module.exports = {
    formatTime: formatTime,
    request: request,
    checkLogin: checkLogin,
    userthree: userthree,
    r: r,
    updateUserinfo: updateUserinfo,
    errMsg: errMsg,
    sucMsg: sucMsg
}
